package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.CheckinAssessClassDTO;
import com.api.shopapi.entity.dto.CheckinAssessGroupDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * 评估组
 */
@RestController
@RequestMapping("CheckinAssessGroup")
public class CheckinAssessGroupController extends BaseController {

    @Autowired
    @Qualifier("CheckinAssessGroupServiceImplOne")
    private CheckinAssessGroupService checkinAssessGroupService;

    @Autowired
    @Qualifier("CheckinAssessItemServiceImplOne")
    private CheckinAssessItemService checkinAssessItemService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("CheckinAssessClassServiceImplOne")
    private CheckinAssessClassService checkinAssessClassService;



    @Autowired
    @Qualifier("InstitutionStaffRelServiceImplOne")
    private InstitutionStaffRelService institutionStaffRelService;



    @RequestMapping(value = "Add", method = RequestMethod.POST)
    public MyResult Insert(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String groupName = jsonObject.getString("groupName");
            String checkinAssessClassCode = jsonObject.getString("checkinAssessClassCode");
            int sort = jsonObject.getInteger("sort");
            JSONArray assessItemArr = jsonObject.getJSONArray("assessItemArr");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessGroup/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            CheckinAssessGroup checkinAssessGroup = checkinAssessGroupService.getByNameAndInstitutionCode(groupName, institutionCode);
            if (checkinAssessGroup != null) {
                return failed("此评估组已存在");
            }
            if (assessItemArr.size() == 0) {
                return failed("此评估组的项为空");
            }
            checkinAssessGroup = new CheckinAssessGroup();
            String checkinAssessGroupCode = UUID.randomUUID().toString();

            checkinAssessGroup.setCheckinAssessGroupCode(checkinAssessGroupCode);
            checkinAssessGroup.setName(groupName);
            checkinAssessGroup.setSort(sort);
            checkinAssessGroup.setStaffCode(staffCode);
            checkinAssessGroup.setInstitutionCode(institutionCode);
            checkinAssessGroup.setCheckinAssessClassCode(checkinAssessClassCode);

            checkinAssessGroupService.insert(checkinAssessGroup, assessItemArr);
            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/CheckinAssessGroup/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    @RequestMapping(value = "Edit", method = RequestMethod.POST)
    public MyResult update(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String checkinAssessGroupCode = jsonObject.getString("checkinAssessGroupCode");
            String groupName = jsonObject.getString("groupName");
            int sort = jsonObject.getInteger("sort");
            int status = jsonObject.getInteger("status");
            JSONArray assessItemArr = jsonObject.getJSONArray("assessItemArr");
            String checkinAssessClassCode = jsonObject.getString("checkinAssessClassCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessGroup/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();
            CheckinAssessGroup checkinAssessGroup = checkinAssessGroupService.getByNameAndInstitutionCode(groupName, institutionCode);
            if (checkinAssessGroup != null) {
                if (!checkinAssessGroup.getCheckinAssessGroupCode().equals(checkinAssessGroupCode)) {
                    return failed("此评估已存在");
                }
            }
            checkinAssessGroup = checkinAssessGroupService.getByCheckinAssessGroupCode(checkinAssessGroupCode);
            if (checkinAssessGroup == null) {
                return failed("数据不存在");
            }

            checkinAssessGroup.setName(groupName);
            checkinAssessGroup.setSort(sort);
            checkinAssessGroup.setStatus(status);
            checkinAssessGroup.setCheckinAssessClassCode(checkinAssessClassCode);
            checkinAssessGroupService.updateByCheckinAssessGroupCode(checkinAssessGroup, assessItemArr);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/CheckinAssessGroup/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //ins
    @RequestMapping(value = "Page", method = RequestMethod.POST)
    public MyResult page(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int status = jsonObject.getInteger("status");
            String checkinAssessClassCode = jsonObject.getString("checkinAssessClassCode");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessGroup/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            PageInfo<CheckinAssessGroup> pageInfocheckinAssessGroup = checkinAssessGroupService.getPageList(pageIndex, pageSize, institutionCode, status, checkinAssessClassCode, name);
            List<CheckinAssessGroupDTO> checkinAssessGroupDTOList = new ArrayList<>();
            for (CheckinAssessGroup checkinAssessGroup : pageInfocheckinAssessGroup.getList()) {
                CheckinAssessGroupDTO checkinAssessGroupDTO = mapper.map(checkinAssessGroup, CheckinAssessGroupDTO.class);
                checkinAssessGroupDTOList.add(checkinAssessGroupDTO);
            }
            for (CheckinAssessGroupDTO checkinAssessGroupDTO : checkinAssessGroupDTOList) {
                CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByCheckinAssessClassCode(checkinAssessGroupDTO.getCheckinAssessClassCode());
                checkinAssessGroupDTO.setCheckinAssessClass(checkinAssessClass);
                List<CheckinAssessItem> checkinAssessItemList = checkinAssessItemService.getByCheckinAssessGroupCode(checkinAssessGroupDTO.getCheckinAssessGroupCode());
                checkinAssessGroupDTO.setCheckinAssessItemList(checkinAssessItemList);
            }
            pageInfocheckinAssessGroup.setList(Collections.unmodifiableList(checkinAssessGroupDTOList));

            return success(pageInfocheckinAssessGroup);
        } catch (Exception e) {
            insertError("/CheckinAssessGroup/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }

    //gov
    @RequestMapping(value = "GovPage", method = RequestMethod.POST)
    public MyResult GovPage(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String name = jsonObject.getString("name");
            int pageIndex = jsonObject.getInteger("pageIndex");
            int status = jsonObject.getInteger("status");
            String checkinAssessClassCode = jsonObject.getString("checkinAssessClassCode");
            int pageSize = jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessGroup/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<CheckinAssessGroup> pageInfocheckinAssessGroup = checkinAssessGroupService.pageByGovCode(pageIndex, pageSize,
                    staff.getGovernmentCode(), status, checkinAssessClassCode, name);

            List<CheckinAssessGroupDTO> checkinAssessGroupDTOList = new ArrayList<>();

            for (CheckinAssessGroup checkinAssessGroup : pageInfocheckinAssessGroup.getList()) {
                CheckinAssessGroupDTO checkinAssessGroupDTO = mapper.map(checkinAssessGroup, CheckinAssessGroupDTO.class);
                checkinAssessGroupDTOList.add(checkinAssessGroupDTO);
            }

            for (CheckinAssessGroupDTO checkinAssessGroupDTO : checkinAssessGroupDTOList) {
                CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByCheckinAssessClassCode(checkinAssessGroupDTO.getCheckinAssessClassCode());
                checkinAssessGroupDTO.setCheckinAssessClass(checkinAssessClass);
                List<CheckinAssessItem> checkinAssessItemList = checkinAssessItemService.getByCheckinAssessGroupCode(checkinAssessGroupDTO.getCheckinAssessGroupCode());
                checkinAssessGroupDTO.setCheckinAssessItemList(checkinAssessItemList);
            }

            pageInfocheckinAssessGroup.setList(Collections.unmodifiableList(checkinAssessGroupDTOList));

            return success(pageInfocheckinAssessGroup);
        } catch (Exception e) {
            insertError("/CheckinAssessGroup/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "List", method = RequestMethod.POST)
    public MyResult list(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessGroup/List");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            List<CheckinAssessGroup> groupServicePageList = checkinAssessGroupService.getList(institutionCode);
            List<CheckinAssessGroupDTO> checkinAssessGroupDTOList = new ArrayList<>();
            for (CheckinAssessGroup checkinAssessGroup : groupServicePageList) {
                CheckinAssessGroupDTO checkinAssessGroupDTO = mapper.map(checkinAssessGroup, CheckinAssessGroupDTO.class);
                checkinAssessGroupDTOList.add(checkinAssessGroupDTO);
            }
            for (CheckinAssessGroupDTO checkinAssessGroupDTO : checkinAssessGroupDTOList) {
                CheckinAssessClass checkinAssessClass = checkinAssessClassService.getByCheckinAssessClassCode(checkinAssessGroupDTO.getCheckinAssessClassCode());
                checkinAssessGroupDTO.setCheckinAssessClass(checkinAssessClass);
                List<CheckinAssessItem> checkinAssessItemList = checkinAssessItemService.getByCheckinAssessGroupCode(checkinAssessGroupDTO.getCheckinAssessGroupCode());
                checkinAssessGroupDTO.setCheckinAssessItemList(checkinAssessItemList);
            }
            return success(checkinAssessGroupDTOList);
        } catch (Exception e) {
            insertError("/CheckinAssessGroup/List", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    /**
     * 根据分类查询组和项
     */
    @RequestMapping(value = "GetListByClass", method = RequestMethod.POST)
    public MyResult GetListByClass(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign, staffCode,"/CheckinAssessGroup/GetListByClass");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            InstitutionStaffRel institutionStaffRel = institutionStaffRelService.getByStaffCode(staffCode);
            String institutionCode = institutionStaffRel.getInstitutionCode();

            List<CheckinAssessClass> checkinAssessClassList = checkinAssessClassService.getList(institutionCode);

            List<CheckinAssessClassDTO> checkinAssessClassDTOList = new ArrayList<>();
            for (CheckinAssessClass checkinAssessClass : checkinAssessClassList) {
                CheckinAssessClassDTO checkinAssessClassDTO = mapper.map(checkinAssessClass, CheckinAssessClassDTO.class);
                checkinAssessClassDTOList.add(checkinAssessClassDTO);
            }

            for (CheckinAssessClassDTO checkinAssessClassDTO : checkinAssessClassDTOList) {
                List<CheckinAssessGroup> checkinAssessGroupList = checkinAssessGroupService.getByCheckinAssessClassCode(checkinAssessClassDTO.getCheckinAssessClassCode());
                List<CheckinAssessGroupDTO> checkinAssessGroupDTOList = new ArrayList<>();
                for (CheckinAssessGroup checkinAssessGroup : checkinAssessGroupList) {
                    CheckinAssessGroupDTO checkinAssessGroupDTO = mapper.map(checkinAssessGroup, CheckinAssessGroupDTO.class);
                    checkinAssessGroupDTOList.add(checkinAssessGroupDTO);
                }
                for (CheckinAssessGroupDTO checkinAssessGroupDTO : checkinAssessGroupDTOList) {
                    List<CheckinAssessItem> checkinAssessItemList = checkinAssessItemService.getByCheckinAssessGroupCode(checkinAssessGroupDTO.getCheckinAssessGroupCode());
                    checkinAssessGroupDTO.setCheckinAssessItemList(checkinAssessItemList);
                }
                checkinAssessClassDTO.setCheckinAssessGroupDTOList(checkinAssessGroupDTOList);
            }

            return success(checkinAssessClassDTOList);
        } catch (Exception e) {
            insertError("/CheckinAssessGroup/GetListByClass", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }


    //根据分类查询分类下的组
    @RequestMapping(value = "ListByClassCode", method = RequestMethod.POST)
    public MyResult ListByClassCode(@RequestBody JSONObject jsonObject) {
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String checkinAssessClassCode = jsonObject.getString("checkinAssessClassCode");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/CheckinAssessGroup/GetListByClass");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }


            List<CheckinAssessGroup> checkinAssessGroupList = checkinAssessGroupService.getByCheckinAssessClassCode(checkinAssessClassCode);

            List<CheckinAssessGroupDTO> checkinAssessGroupDTOList = new ArrayList<>();
            for (CheckinAssessGroup checkinAssessGroup : checkinAssessGroupList) {
                CheckinAssessGroupDTO checkinAssessGroupDTO = mapper.map(checkinAssessGroup, CheckinAssessGroupDTO.class);
                checkinAssessGroupDTOList.add(checkinAssessGroupDTO);
            }

            for (CheckinAssessGroupDTO checkinAssessGroupDTO : checkinAssessGroupDTOList) {
                List<CheckinAssessItem> checkinAssessItemList = checkinAssessItemService.getByCheckinAssessGroupCode(checkinAssessGroupDTO.getCheckinAssessGroupCode());
                checkinAssessGroupDTO.setCheckinAssessItemList(checkinAssessItemList);
            }

            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("list",checkinAssessGroupDTOList);

            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/CheckinAssessGroup/ListByClassCode", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }

    }



}
